package com.ruoyi.system.service.impl;

import cn.hutool.core.collection.IterUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.system.domain.base.BaseSerialNumber;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.mapper.BaseSerialNumberMapper;
import com.ruoyi.system.service.IBaseSerialNumberService;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * 流水号规则Service业务层处理
 *
 * @author ruoyi
 * @date 2023-02-16
 */
@Service
public class BaseSerialNumberServiceImpl extends ServiceImpl<BaseSerialNumberMapper, BaseSerialNumber> implements IBaseSerialNumberService {

    @Override
    public String getSerialNumber(String prefix) {
        String number = "000001";//初始号
        if (StringUtils.isBlank(prefix)) {
            throw new CustomException("请提供单据号前缀");
        }
        //当前日期戳
        String dateStamp = this.getNowDateStamp();

        BaseSerialNumber serialNumber = new BaseSerialNumber();
        //按前缀、日期戳 获取流水号配置
        List<BaseSerialNumber> baseSerialNumber = baseMapper.selectList(new QueryWrapper<BaseSerialNumber>().eq("prefix",prefix).eq("date_stamp",dateStamp).orderByDesc("code"));
        if (IterUtil.isNotEmpty(baseSerialNumber)) {//已有则获取并更新
            serialNumber = baseSerialNumber.get(0);
            number = serialNumber.getCode();//当前流水号
            //流水号+1
            number = String.valueOf(Integer.parseInt(number) + 1);
            //前缀补0补齐5位
            String str = "000000";
            number = str.substring(0, 6 - number.length()) + number;
            serialNumber.setCode(number);
            serialNumber.setUpdateTime(new Date());
        } else {//没有则新增
            serialNumber.setPrefix(prefix);
            serialNumber.setDateStamp(dateStamp);
            serialNumber.setCode(number);
            serialNumber.setCreateTime(new Date());
        }
        baseMapper.insert(serialNumber);
        //拼接
        number = prefix + dateStamp + number;
        return number;
    }

    /**
     * 当前日期戳
     * @return
     */
    public static String getNowDateStamp() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        String todayTime = formatter.format(new Date());
        return todayTime;
    }

    @Override
    public String getSerialNumber(String prefix, Integer length) {
        String baseNumber = "";//按位数生成初始号
        for (Integer i = 1; i < length; i++) {
            baseNumber += "0";
        }
        baseNumber += "1";

        String nextNumber = "";
        if (StringUtils.isBlank(prefix)) {
            throw new CustomException("请提供单据号前缀");
        }
        //当前日期戳
        String dateStamp = this.getNowDateStamp();
        BaseSerialNumber serialNumber = new BaseSerialNumber();
        //按前缀 获取流水号配置
        List<BaseSerialNumber> baseSerialNumber = baseMapper.selectList(new QueryWrapper<BaseSerialNumber>().eq("prefix",prefix).eq("date_stamp",dateStamp).orderByDesc("id"));
        if (IterUtil.isNotEmpty(baseSerialNumber)) {//已有则获取并更新
            serialNumber = baseSerialNumber.get(0);
            nextNumber = serialNumber.getCode();//当前流水号
            //流水号+1
            nextNumber = String.valueOf(Integer.parseInt(nextNumber) + 1);
            //前缀补0补齐7位
            String str = baseNumber;
            nextNumber = str.substring(0, length - nextNumber.length()) + nextNumber;
            serialNumber.setCode(nextNumber);
            serialNumber.setUpdateTime(new Date());
        } else {//没有则新增
            nextNumber = baseNumber;
            serialNumber.setPrefix(prefix);
            serialNumber.setDateStamp(dateStamp);
            serialNumber.setCode(nextNumber);
            serialNumber.setCreateTime(new Date());
        }
        baseMapper.insert(serialNumber);
        //拼接
        nextNumber = prefix + dateStamp + nextNumber;
        return nextNumber;
    }

}
